package situ.autoweter.service.impl;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import situ.autoweter.entity.billList;
import situ.autoweter.entity.blist;
import situ.autoweter.service.PyBillService;
import situ.autoweter.dao.PyBillDao;
import situ.autoweter.common.Assist;
import situ.autoweter.entity.PyBill;
/**
 * PyBill的服务接口的实现类
 *
 * @author
 *
 */
@Service
public class PyBillServiceImpl implements PyBillService {
    private final Logger LOG = LogManager.getLogger(this.getClass());

    @Autowired
    private PyBillDao pyBillDao;
    // TODO 当你看到这个方法时你应该创建一个工具类做通用的方法,定义自己的返回格式化
    private static final int C200 = 200;
    private static final int C412 = 412;
    public String resultFormat(int code, Object data) {
        JSONObject result = new JSONObject();
        result.put("code", code);
        if (data != null) {
            result.put("data", data);
        }
        return result.toJSONString();
    }

    @Override
    public List<PyBill> find(PyBill value) {
        //TODO这里可以做通过Assist做添加查询
        List<PyBill> result = pyBillDao.selectPyBill(null);
        if (LOG.isDebugEnabled()) {
            LOG.debug("执行获取PyBill数据集-->结果:", result);
        }
        return result;
    }
    @Override
    public PyBill findOne(String id) {
        if (id == null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("执行通过PyBill的id获得PyBill对象-->失败:id不能为空");
            }
            return null;
        }
        PyBill result = pyBillDao.selectPyBillById(id);
        if (LOG.isDebugEnabled()) {
            LOG.debug("执行通过PyBill的id获得PyBill对象-->结果:", result);
        }
        return result;
    }

    @Override
    public String saveNotNull(PyBill value) {
        if (value == null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("执行将PyBill中属性值不为null的数据保存到数据库-->失败:对象不能为空");
            }
            return resultFormat(C412, null);
        }
        int result = pyBillDao.insertNotNullPyBill(value);
        if (LOG.isDebugEnabled()) {
            LOG.debug("执行将PyBill中属性值不为null的数据保存到数据库-->结果:", result);
        }
        return resultFormat(C200, result);
    }
    @Override
    public String updateNotNullById(PyBill value) {
        if (value == null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("执行通过PyBill的id更新PyBill中属性不为null的数据-->失败:对象为null");
            }
            return resultFormat(C412, null);
        }
        int result = pyBillDao.updateNotNullPyBillById(value);
        if (LOG.isDebugEnabled()) {
            LOG.debug("执行通过PyBill的id更新PyBill中属性不为null的数据-->结果:", result);
        }
        return resultFormat(C200, result);
    }

    @Override
    public String deleteById(String id) {
        if (id == null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("执行通过PyBill的id删除PyBill-->失败:id不能为空");
            }
            return resultFormat(C412, null);
        }
        int result = pyBillDao.deletePyBillById(id);
        if (LOG.isDebugEnabled()) {
            LOG.debug("执行通过PyBill的id删除PyBill-->结果:", result);
        }
        return resultFormat(C200, result);
    }

    @Override
    public List<Map> query() {
        return pyBillDao.query();
    }

    @Override
    public Integer queryInvoiceNo() {
        return pyBillDao.queryInvoiceNo();
    }

    @Override
    public void updatebill(blist b) {
        for(billList s :b.getBlist() ) {
            if(s.getUserMoney()>0) {
                s.setRumoney(s.getUserMoney() - s.getRealMoney());
                pyBillDao.updatebill(s);
                pyBillDao.updatebill1(s);
            }else{
                pyBillDao.updatebill1(s);
            }
        }
    }
    public List<PyBill> queryByUserNo(String No){
        return pyBillDao.queryByUserNo(No);
    }
}